package com.ethink.framework.common.validate.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.ethink.framework.common.validate.annotation.IsMobileNo;
import com.ethink.framework.common.validate.properties.ValidateProperties;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * IsMobileNo 注解实现类
 *
 * @author name(mobile)
 */
@Service
public class IsMobileNoValidatorImpl implements ConstraintValidator<IsMobileNo, String> {


    /**
     * @see ConstraintValidator#initialize(Annotation)
     */
    @Override
    public void initialize(IsMobileNo constraintAnnotation) {

    }

    /**
     * @see ConstraintValidator#isValid(Object, ConstraintValidatorContext)
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        Matcher match = Pattern.compile(SpringUtil.getBean(ValidateProperties.class).getMobilePattern()).matcher(value);
        return match.matches();
    }

}